package com.fare.taluo.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.city.CityQueryApplication;
import com.fare.taluo.application.plane.city.CityTreeApplicatioin;
import com.fare.taluo.application.plane.city.ComboTree;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.core.domain.config.city.CityEntity;
import com.fare.taluo.vo.core.CityEntityVO;

@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/cityManage")
@ParentPackage("json-default")
public class CityManageAction extends BaseFdcAction{
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private CityTreeApplicatioin cityTreeManager;
	@Autowired
	private CityQueryApplication cityQueryApplication;
	@Autowired
	private PortApplication portApplication;
	private Map<String, Object> dataMap = new HashMap<String, Object>();
	
	private CityEntityVO cityVO;
	
	private Long id;
	private Long pid;
	private Integer treeType;
	
	private int pageSize=10;
	
	private int page = 1;
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	private List<ComboTree> trees;
	
	public List<ComboTree> getTrees() {
		return trees;
	}
	public Map<String, Object> getDataMap() {
		return dataMap;
	}
	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public Long getPid() {
		return pid;
	}
	public void setPid(Long pid) {
		this.pid = pid;
	}
	
	public Integer getTreeType() {
		return treeType;
	}
	public void setTreeType(Integer treeType) {
		this.treeType = treeType;
	}
	
	@Action(value="toJsp",results={@Result(name="success", type="dispatcher",location="/fare/cityManage/city_manager.jsp")})
	public String toJsp() throws Exception {
		return SUCCESS;
	}
	
	@Action(value="tree",results={@Result(name="success", type="json",params={"root","trees"})})
	public String tree() throws Exception {
		trees = new ArrayList<ComboTree>();
		trees.add(cityTreeManager.getTopCityTree());
		return SUCCESS;
	}
	/**
	 * 列表页面
	 * @return
	 */
	@Action(value="list",results={@Result(name="city", type="dispatcher",location="/fare/cityManage/city_list.jsp"),
			@Result(name="port", type="dispatcher",location="/fare/cityManage/port_list.jsp")})
	public String list(){
		if(treeType == CityEntity.CITY){
			return "port";
		}
		return "city";
	}
	/**
	 * 列表页面
	 * @return
	 */
	@Action(value="listJSON",results={@Result(name="success", type="json",params={"root","dataMap"})})
	public String listJSON(){
		if(pid == null) pid = 1L;
		if(CityEntity.CITY == treeType){//显示机场
			dataMap.put("Rows", portApplication.findByCityId(pid));
		}else{//显示子区域
			Page<CityEntityVO> all=cityQueryApplication.pageQuery(pid, page, pageSize);
			dataMap.put("Rows", all.getResult());
			dataMap.put("start", page * pageSize - pageSize);
			dataMap.put("limit", pageSize);
			dataMap.put("Total", all.getTotalCount());
		}
		return SUCCESS;
	}
	
	@Action(value="edit",
			results={@Result(name="success", type="dispatcher", 
			location="/fare/cityManage/city_edit.jsp")})
	public String edit(){
		cityVO =new CityEntityVO();
		
		if(null!=id && id >0){
			cityVO =cityQueryApplication.findById(id);
		}
		if(null!=pid && pid>0){
			cityVO.setPid(pid);
		}
		return SUCCESS;
	}
	
	@Action(value="creatCtityScript",results={@Result(name="success", type="json",params={"root","dataMap"})})
	public String creatCtityScript(){
		try {
			cityQueryApplication.createCitySelect();
			dataMap.put("msg", "生成城市选择框成功！");
		} catch (Exception e) {
			dataMap.put("msg", "生成城市选择框失败！");
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@Action(value="saveCity",results={@Result(name="success", type="json")})
	public String saveCity(){
		cityVO.setCreateDate(new Date());
		cityQueryApplication.saveOrUpdate(cityVO);
		return SUCCESS;
	}
	
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public CityEntityVO getCityVO() {
		return cityVO;
	}
	public void setCityVO(CityEntityVO cityVO) {
		this.cityVO = cityVO;
	}
	
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}
